<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Name_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
	$this->load->helper('url');
	$this->load->database();
    }

    function all_user()
    {
        $query = $this->db->query("SELECT * FROM fbapp_fb WHERE 1");
        return $query->result_array();
    }
    function all_newfeed()
    {
        $query = $this->db->query("SELECT * FROM fbapp_post WHERE 1 ORDER BY `post_timestamp` DESC ");
        return $query->result_array();
    }
    function filter_fb($fb_username)
    {
        $query = $this->db->query("SELECT * FROM fbapp_fb WHERE fb_username ='".$fb_username."'");
        return $query->result_array();
    }
    function filter_post_wall($fb_id)
    {
        $query = $this->db->query("SELECT * FROM fbapp_post WHERE fb_id_wall =".$fb_id." ORDER BY `post_timestamp` DESC ");
        return $query->result_array();
    }

    function filter_post($post_id)
    {
        $query = $this->db->query("SELECT * FROM fbapp_post WHERE post_id =".$post_id);
        return $query->result_array();
    }

    function filter_comment($post_id)
    {
        $query = $this->db->query("SELECT * FROM fbapp_com WHERE post_id =".$post_id." ORDER BY `com_timestamp` ASC ");
        return $query->result_array();
    }

    function filter_comment_like($com_id)
    {
        $query = $this->db->query("SELECT * FROM fbapp_likec WHERE com_id =".$com_id);
	return $query->result_array();
    }

    function filter_like($post_id)
    {
        $query = $this->db->query("SELECT * FROM fbapp_likep WHERE post_id =".$post_id);
	return $query->result_array();
    }
    function filter_like_fb($post_id, $fb_id)
    {
        $query = $this->db->query("SELECT * FROM fbapp_likep WHERE post_id =".$post_id." AND fb_id =".$fb_id);

        if($query->num_rows() > 0):
            return "TRUE";
        else:
            return "FALSE";
        endif;
    }
    function filter_like_fb_comment($com_id, $fb_id)
    {
        $query = $this->db->query("SELECT * FROM fbapp_likec WHERE com_id =".$com_id." AND fb_id =".$fb_id);

        if($query->num_rows() > 0):
            return "TRUE";
        else:
            return "FALSE";
        endif;
    }
    function get_username($fb_id)
    {
        $query = $this->db->query("SELECT fb_username FROM fbapp_fb WHERE fb_id =".$fb_id);

        if($query->num_rows() > 0):
            foreach($query->result() as $row):
                $data[] = $row->fb_username;
            endforeach;
            return $data[0];
        else:
            return false;
        endif;
    }

    function get_fb_pic($fb_id)
    {
        $query = $this->db->query("SELECT fb_pic FROM fbapp_fb WHERE fb_id =".$fb_id);

        if($query->num_rows() > 0):
            foreach($query->result() as $row):
                $data[] = $row->fb_pic;
            endforeach;
            return $data[0];
        else:
            return false;
        endif;
    }

    function insert_post($data)
    {
	$this->db->insert('fbapp_post',$data);
	return true;
    }
    
    function insert_comment($data)
    {
	$this->db->insert('fbapp_com',$data);
	return true;
    }

    function insert_likep($data)
    {
	$this->db->insert('fbapp_likep',$data);
	return true;
    }

    function insert_likec($data)
    {
	$this->db->insert('fbapp_likec',$data);
	return true;
    }

    function delete_likep($data)
    {
	$str = 'fb_id = '.$data['fb_id'].' AND post_id ='.$data['post_id'];
        $this->db->where($str);
        $this->db->delete('fbapp_likep'); 
    }
    function delete_likec($data)
    {
	$str = 'fb_id = '.$data['fb_id'].' AND com_id ='.$data['com_id'];
        $this->db->where($str);
        $this->db->delete('fbapp_likec'); 
    }

    function get_new_post_data($fb_id)
    {
        $query = $this->db->query("SELECT * FROM fbapp_post WHERE fb_id_poster =".$fb_id." ORDER BY post_timestamp DESC LIMIT 1");
	return $query->result_array();	
    }
}
?>
